//
//  SecondViewController.m
//  UI15_XML_JSON数据结构解析
//
//  Created by Scott on 15/5/15.
//  Copyright (c) 2015年 www.lanou3g.com 蓝欧3G. All rights reserved.
//

#import "SecondViewController.h"
#import "Student.h"

@interface SecondViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation SecondViewController

- (void)dealloc
{
    [_arr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"JSON Parse";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self handleData];
    [self tableViewSubview];

}

#pragma mark - 知识点4 JSON 解析
- (void)handleData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSLog(@"%@", data);
    
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:&error];
    
    NSLog(@"%@", result);
    
    /* 使用数组重新接收数据 */
    self.arr = [NSMutableArray array];
    
    for (NSDictionary *dic in result) {
        
        /* 使用model类来接收数据, 放入数组中 */
        
        Student *stu = [[Student alloc] init];
        
        [stu setValuesForKeysWithDictionary:dic];
        
        [self.arr addObject:stu];
        [stu release];
    
    }
    
    NSLog(@"%@", self.arr);
    
}


#pragma mark - TableView
- (void)tableViewSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"reuse"] autorelease];
    }
    
    Student *stu = [self.arr objectAtIndex:indexPath.row];
    
    cell.textLabel.text = stu.name;
    cell.detailTextLabel.text = stu.phone;
    
    return cell;
    
    
}





#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
